﻿#include "Login.h"
#include "..\Data\User.h"

namespace TC
{
    namespace Request
    {
        void Login::login(const QString &username, const QString &password)
        {
            if (status() == Status::Communicating)
                return;
            AbstractRemoteProcedure::get("user/login",
            {
                {"username", username},
                {"password", password}
            });
        }

        void Login::touristLogin()
{
            if (status() == Status::Communicating)
                return;
            AbstractRemoteProcedure::get("user/login",
            {
                {"username", "tourist"},
                {"password", ""}
            });
        }

        void Login::onDataReady(QDataStream &stream)
        {
            bool result;
            stream >> result;
            if (result)
            {
                QByteArray signature;
                stream >> signature;

                bool isTourist, isUser, isAdmin;
                stream >> isTourist >> isUser >> isAdmin;
                
				Data::User::instance()->setSignature(signature);
				Data::User::instance()->setIsTourist(isTourist);
				Data::User::instance()->setIsUser(isUser);
				Data::User::instance()->setIsAdmin(isAdmin);

                setOk();
            }
            else
                setFailed();
        }
    }
}
